package indzcraft.ui;

import indzcraft.loader.Equipment;
import indzcraft.loader.ItemEquiped;
import indzcraft.ranking.Database;
import indzcraft.ranking.EP;
import indzcraft.ranking.ItemCompare;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;

public class ItemPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private IndzCraftUI icui = null;
	private int slot;
	private String slotName;
	private ItemEquiped item;
		
	List<List<Integer>> slotTypes = new ArrayList<List<Integer>>();
	
	public ItemPanel(IndzCraftUI ui,int slot){
		super();
		
		// fill slotTypes
		slotTypes.add(0, Arrays.asList(1) );
		slotTypes.add(1, Arrays.asList(2) );
		slotTypes.add(2, Arrays.asList(3) );
		slotTypes.add(3, Arrays.asList(16) );
		slotTypes.add(4, Arrays.asList(5) );
		//
		slotTypes.add(5, Arrays.asList(4) );
		slotTypes.add(6, Arrays.asList(19) );
		slotTypes.add(7, Arrays.asList(9) );
		
		slotTypes.add(8, Arrays.asList(10) );
		slotTypes.add(9, Arrays.asList(6) );
		slotTypes.add(10, Arrays.asList(7) );
		slotTypes.add(11, Arrays.asList(8) );
		
		slotTypes.add(12, Arrays.asList(11) );
		slotTypes.add(13, Arrays.asList(11) );
		slotTypes.add(14, Arrays.asList(12) );
		slotTypes.add(15, Arrays.asList(12) );
		
		slotTypes.add(16, Arrays.asList(13,21) );
		slotTypes.add(17, Arrays.asList(13,14,22) );
		slotTypes.add(18, Arrays.asList(15,25,26) );
		
		this.icui = ui;
		this.slot = slot;
		this.slotName = Slot.possibleSlots[slot].getName();
		this.item = icui.getLoader().getEquipment().get(slotName);
				
		setLayout(new GridBagLayout());
        
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.HORIZONTAL;
		
		Database db = icui.getLoader().getDatabase();
		ItemButton ib = null;
		ArrayList<ItemEquiped> items = db.findAllWithInventoryType(this.slotName,(Integer[])slotTypes.get(this.slot).toArray());
		ItemCompare<ItemEquiped> ic = new ItemCompare<ItemEquiped>(icui,item,items);
		ArrayList<EP<ItemEquiped>> itemep = ic.getComperator();
		Iterator<EP<ItemEquiped>> it = itemep.iterator();
		int j = 0;
		double max = 0;
		ItemEquiped curItem = icui.getLoader().getEquipment().get(this.slotName);
		while (it.hasNext()) {
			ItemEquiped item = it.next().getItem();
			if(max == 0) max = item.getEP();
			ib = new ItemButton(item.getId(),this.slot);
			
			c.anchor = GridBagConstraints.WEST;
            c.insets = new Insets(0, 0, 0, 7);
            c.gridx = 0; c.gridy = j; c.gridheight = 3; c.gridwidth = 1;
            if (item == curItem)
				ib.setSelected(true);
            add(ib, c);
            
            JLabel jl = new JLabel();
            File file = new File("./icons/"+item.getIcon()+".png");
            Image image;
			try {
				if(file.exists())
					image = ImageIO.read(file);
				else
					image = ImageIO.read(new File("./icons/inv_misc_questionmark.png"));
				image = createImage(new FilteredImageSource(image.getSource(),
						new CropImageFilter(5, 5, 54, 54)));
				jl.setIcon(new ImageIcon(image));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            c.insets = new Insets(0, 0, 0, 7);
            c.gridx = 1; c.gridy = j; c.gridheight = 3; c.gridwidth = 1;
            add(jl, c);
            
            String name = item.getName();
            if (name.length()>35)
                    name = name.substring(0,32)+"...";
            JLabel label = new JLabel(name);
            c.insets = new Insets(0, 0, 0, 0);
            c.gridx = 2; c.gridy = j; c.gridheight = 1;
            add(label, c);
            
            String lvl = ""+item.getItemLevel();
            if(item.getHeroic() == 1)
            	lvl += " heroic";
            label = new JLabel(String.format("Level: %s", lvl));
            c.gridx = 2; c.gridy = j+1;
            add(label, c);
            
            label = new JLabel(String.format("%.2f", item.getEP()));
            label.setHorizontalAlignment(JLabel.RIGHT);
            c.gridx = 3; c.gridy = j;
            add(label, c);
            
            double diff;
            if (curItem != null)
                diff = item.getEP() - curItem.getEP();
            else
                diff = item.getEP();
            label = new JLabel(String.format("%+.2f", diff));
            label.setHorizontalAlignment(JLabel.RIGHT);
            c.gridx = 3; c.gridy = j+1;
            add(label, c);
            
            // bar
            c.insets = new Insets(5, 0, 0, 0);
            c.gridx = 2; c.gridy = j+2; c.gridwidth = 2;
            add(new RatingPanel(item.getEP(), max, curItem.getEP()), c);
            
            if (it.hasNext()) {
                JSeparator sep = new JSeparator();
                c.insets = new Insets(3, 0, 3, 0);
                c.gridx = 1; c.gridy = j+3; c.gridwidth = 3;
                add(sep, c);
                j += 1;
            }
            
            j += 3;
		}
		setBorder(new EmptyBorder(new Insets(3,6,6,6)));
        setVisible( true );
	}
	
	public class ItemButton extends JRadioButton implements ActionListener {
        
		private static final long serialVersionUID = 1L;
		private int id;
		private int slot;
		private ItemEquiped item;
        
        public ItemButton(int id,int slot) {
        	this.slot = slot;
    		this.id = id;
            setFocusable(false);
            addActionListener(this);
            setVisible(true);
            
            setLayout(null);
            
            item = (ItemEquiped) icui.db.get(id);
            if(item == null){
            	System.out.println("item is null, big fehler");
            }
        }

        public void actionPerformed(ActionEvent e) {
        	
        	System.out.println(this.id);
        	Equipment e1 = icui.getLoader().getEquipment();
        	e1.put(Slot.possibleSlots[slot].getName(), item);
        	icui.setItemSlot(this.slot, item);
        	icui.showItemPanel(this.slot);
        	icui.showStats();
        }

	}
}
